package com.example.restatr.Atcivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.example.restatr.Adapter.NewsAdapter;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Main.Fragment.NewsFragment;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class NewsAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private TabLayout tab;
    public static ViewPager XwPager;
    public static List<String> tabTitle = new ArrayList<>();
    public static List<String> tabId = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private Intent intent;
    public static String NewsLink, LikeLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_xw_at );
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace( R.id.XwAtPager, new NewsFragment() ).commit();
        }
        tb = findViewById( R.id.toolbar);
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        tab = findViewById( R.id.tab );
        XwPager = findViewById( R.id.XwAtPager );

        Tools.SetBarTitle( tb, barTitle, "新闻资讯", search, NewsAt.this,false );


        if (tabTitle.size() > 0) {
            tabTitle.clear();
            tabId.clear();
        }
        intent = getIntent();
        getPagerTitle( intent.getStringExtra( "Link" ) );
        MainActivity.info = intent.getStringExtra( "info" );
        NewsLink = intent.getStringExtra( "NewsLink" );
        LikeLink = intent.getStringExtra( "LikeLink" );
        if (intent.getStringExtra( "info" ).equals( "dt" )) {
            tab.setTabMode( TabLayout.MODE_FIXED );
        }
    }

    //获取新闻类别
    private void getPagerTitle(final String link) {

        new Thread( new Runnable() {
            @Override
            public void run() {
                final String tabList = Tools.Get( link, "" );
                if (tabList != null) {
                    runOnUiThread( new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jsonObject2 = new JSONObject( tabList );
                                JSONArray data = jsonObject2.getJSONArray( "data" );
                                for (int i = 0; i < data.length(); i++) {
                                    JSONObject all = data.getJSONObject( i );
                                    tabTitle.add( all.getString( "name" ) );
                                    tabId.add( all.getString( "id" ) );
                                    fragments.add( new NewsFragment() );
                                }
                                if (getFragmentManager() != null) {
                                    XwPager.setAdapter( new NewsAdapter( getSupportFragmentManager(), fragments, tabTitle ) );
                                }
                                XwPager.setOffscreenPageLimit( tabTitle.size() );
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    } );
                }
            }
        } ).start();
        tab.setupWithViewPager( XwPager );
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        MainActivity.info = null;
    }
}